<?php
/* SVN FILE: $Id: app_model.php 6311 2008-01-02 06:33:52Z phpnut $ */

/**
 * Application model for Cake.
 *
 * This file is application-wide model file. You can put all
 * application-wide model-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
 * Copyright 2005-2008, Cake Software Foundation, Inc.
 *								1785 E. Sahara Avenue, Suite 490-204
 *								Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package			cake
 * @subpackage		cake.app
 * @since			CakePHP(tm) v 0.2.9
 * @version			$Revision: 6311 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2008-01-02 06:33:52 +0000 (Wed, 02 Jan 2008) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Application model for Cake.
 *
 * Add your application-wide methods in the class below, your models
 * will inherit them.
 *
 * @package		cake
 * @subpackage	cake.app
 */
class AppModel extends Model{

	 /**
	  * Updates the sum and counter cache of belongsTo associations after a save or delete operation
	  *
	  * @param array $keys Optional foreign key data, defaults to the information $this->data
	  * @param boolean $created True if a new record was created, otherwise only associations with
	  *                                  'counterScope' defined get updated
	  * @return void
	  * @access public
	  */
	function updateCounterCache($keys = array(), $created = false) {

		parent::updateCounterCache($keys);

		if (empty($keys)) {
			$keys = $this->data[$this->alias];
		}

		foreach ($this->belongsTo as $parent => $assoc) {
			if (!empty($assoc['sumCache'])) {
				if ($assoc['sumCache'] === true) {
					$assoc['sumCache'] = Inflector::underscore($this->alias) . '_sum';
				}

				if ($this->{$parent}->hasField($assoc['sumCache'])) {

					$conditions = array($this->escapeField($assoc['foreignKey']) => $keys[$assoc['foreignKey']]);
					if (isset($assoc['sumScope'])) {
						$conditions = array_merge($conditions, (array)$assoc['sumScope']);
					}
					if (!isset($assoc['sumField'])) {
						$assoc['sumField'] = 'amount';
					}
					$fields = 'SUM('.$assoc['sumField'].') AS '.$assoc['sumField'].'';
					$recursive = -1;
					list($edge) = array_values($this->find('first', compact('conditions', 'fields', 'recursive')));

					$sum = ife(empty ($edge[$assoc['sumField']]), 0, $edge[$assoc['sumField']]);

					$this->{$parent}->updateAll(
						array($assoc['sumCache'] => $sum),
						array($this->{$parent}->escapeField() => $keys[$assoc['foreignKey']])
					);

				}
			}
		}

	}


	function afterFind($results, $primary=false) {
         if($primary == true) {
            if(Set::check($results, '0.0')) {
               $fieldName = key($results[0][0]);
                foreach($results as $key=>$value) {
                   $results[$key][$this->alias][$fieldName] = $value[0][$fieldName];
                   unset($results[$key][0]);
                }
             }
         }

         return $results;
}

}
?>